package com.laowang.project.system.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.laowang.common.utils.StringUtils;
import com.laowang.framework.web.page.TableDataInfoMy;
import com.laowang.project.system.domain.WxOrder;
import com.laowang.project.system.domain.ZcConfig;
import com.laowang.project.system.domain.vo.RankVo;
import com.laowang.project.system.service.ZcConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.framework.aspectj.lang.annotation.Log;
import com.laowang.framework.aspectj.lang.enums.BusinessType;

import org.springframework.web.bind.annotation.*;
import com.laowang.project.system.domain.ZcWxOrder;
import com.laowang.project.system.service.ZcWxOrderService;
import com.laowang.framework.web.controller.BaseController;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.common.utils.poi.ExcelUtil;
import com.laowang.framework.web.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * order对象Controller
 * 
 * @author laowang
 * @date 2025-09-09
 */
@Api(tags = "众筹订单对象接口")
@RestController
@RequestMapping("/zcOrder")
public class ZcWxOrderController extends BaseController {
    @Autowired
    private ZcWxOrderService zcWxOrderService;

    @Autowired
    private ZcConfigService zcConfigService;

    @ApiOperation(value = "h5端 排行榜",notes = "")
    @GetMapping("/web/rankList")
    public TableDataInfoMy rankList(
        @RequestParam @ApiParam(value = "页数", required = true,defaultValue = "1") Integer pageNum,
        @RequestParam @ApiParam(value = "条数", required = true,defaultValue = "10") Integer pageSize,
        @RequestParam @ApiParam(value = "1金额排行，2时间排行", required = true,defaultValue = "1") Integer type) {

        startPage();
        List<RankVo> list = zcWxOrderService.selectZcWxOrderRankList(type);
        ZcConfig zcConfig = zcConfigService.selectZcConfigById(20);
        if (zcConfig !=null){
            if (zcConfig.getConfig().equals(2)){
                //2显示名称
                list.forEach(item->{
                    item.setTotalPrice(null);
                });

            }else if (zcConfig.getConfig().equals(3)){
                //2显示金额
                list.forEach(item->{
                    item.setNickname("......");
                });
            }
        }
        return getDataTableMy(list);
    }

    /*
       金额排行 金额大往前排，日期排，时间近往前排
    *    SELECT
            user_id,
            SUM( total_price ) as total_price,
            COUNT( user_id ) AS num ,
            MAX(zwo.create_time) as create_time,
            zwu.gender,
            zwu.nickname
        FROM
            zc_wx_order zwo
            LEFT JOIN zc_wx_user zwu ON zwo.user_id =zwu.id
        WHERE
            zwo.STATUS = '1'
        GROUP BY
            user_id
        ORDER BY
            total_price DESC
    *
    * */

    @ApiOperation(value = "小程序端 下单",notes = "")
    @PostMapping("/payOrder")
    public AjaxResult  payOrder(@RequestBody ZcWxOrder zcWxOrder, HttpServletRequest request) {
        if (Objects.isNull(zcWxOrder.getType())){
            return   AjaxResult.error("类型不能为空");
        }
        if (zcWxOrder.getActivityId() == null || zcWxOrder.getActivityId()==0 ){
            return  AjaxResult.error("活动id 不能为空");
        }
        if (zcWxOrder.getTotalPrice() == null || zcWxOrder.getTotalPrice().compareTo(new BigDecimal(0)) <0 ){
            return  AjaxResult.error("捐款金额 不能为空 ");
        }
        Map<String, Object> map = zcWxOrderService.payOrder(zcWxOrder,request);
        return AjaxResult.success("操作成功",map);
    }

    /**
     * 查询order对象列表
     */
    @ApiOperation(value = "查询order对象列表",notes = "")
    @PreAuthorize("@ss.hasPermi('system:zcOrder:list')")
    @GetMapping("/list")
        public TableDataInfo list(ZcWxOrder zcWxOrder) {
        startPage();
        List<ZcWxOrder> list = zcWxOrderService.selectZcWxOrderList(zcWxOrder);
        return getDataTable(list);
    }
    
    /**
     * 导出order对象列表
     */
    @ApiOperation(value = "导出order对象列表",notes = "")
    @PreAuthorize("@ss.hasPermi('system:zcOrder:export')")
    @Log(title = "order对象", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ZcWxOrder zcWxOrder) {
        List<ZcWxOrder> list = zcWxOrderService.selectZcWxOrderList(zcWxOrder);
        ExcelUtil<ZcWxOrder> util = new ExcelUtil<ZcWxOrder>(ZcWxOrder.class);
        return util.exportExcel(list, "zcOrder");
    }

    /**
     * 获取order对象详细信息
     */
    @ApiOperation(value = "获取order对象详细信息",notes = "")
    @PreAuthorize("@ss.hasPermi('system:zcOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(zcWxOrderService.selectZcWxOrderById(id));
    }

    /**
     * 新增order对象
     */
    @ApiOperation(value = "新增order对象",notes = "")
    @PreAuthorize("@ss.hasPermi('system:zcOrder:add')")
    @Log(title = "order对象", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZcWxOrder zcWxOrder) {
        return toAjax(zcWxOrderService.insertZcWxOrder(zcWxOrder));
    }

    /**
     * 修改order对象
     */
    @ApiOperation(value = "修改order对象",notes = "")
    @PreAuthorize("@ss.hasPermi('system:zcOrder:edit')")
    @Log(title = "order对象", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZcWxOrder zcWxOrder) {
        return toAjax(zcWxOrderService.updateZcWxOrder(zcWxOrder));
    }

    /**
     * 删除order对象
     */
    @ApiOperation(value = "删除order对象",notes = "")
    @PreAuthorize("@ss.hasPermi('system:zcOrder:remove')")
    @Log(title = "order对象", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(zcWxOrderService.deleteZcWxOrderByIds(ids));
    }
}
